package service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import org.eclipse.jdt.internal.compiler.lookup.VariableBinding;

import pojo.Person;
import service.PersonManager;

public class PersonManagerImpl implements PersonManager{
	private static List<Person> persons;
	
	public PersonManagerImpl() {
		persons = new ArrayList<Person>();
	}
	
	@Override
	public Person addPerson(Person person) {
		Person temp = person;
		temp.setId(UUID.randomUUID().toString());
		if(persons.add(person)== true)
		{
			return temp;
		}
		return null;
	}

	@Override
	public List<Person> findAllPerson() {
		return persons;
	}

	@Override
	public int deletePerson(String id) {
		for(int i=0; i<persons.size();i++)
		{
			if(id.equals(persons.get(i).getId())){
				persons.remove(i);
				return 1;
			}
		}
		return 0;
	}

	@Override
	public Person updatePerson(Person person) {
		for(int i=0; i<persons.size();i++)
		{
			if(person.getId().equals(persons.get(i).getId())){
				persons.get(i).setName(person.getName());
				persons.get(i).setSex(person.isSex());
				persons.get(i).setSal(person.getSal());
				persons.get(i).setBirth(person.getBirth());
				return persons.get(i);
			}
		}
		return null;
	}

}
